home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / syscalls / open / openTest.c < prev   
Encoding:
C/C++ Source or Header  |  1990-05-31  |  4.0 KB  |  177 lines

  1. /*
  2.  * openTest.c --
  3.  *    Test of the open system call. 
  4.  *
  5.  *
  6.  *
  7.  */
  8.  
  9. #include <sprite.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <errno.h>
  13. #include <sys/types.h>
  14. #include <sys/param.h>
  15. #include <sys/file.h>
  16.  
  17. #define SHOULD_SUCCEED      0
  18. #define SHOULD_FAIL         1
  19.  
  20. static int errors;
  21. static char *progname;
  22.  
  23. #ifdef __STDC__
  24. static void openTest(void);
  25. static void checkOpen(char *pathname, int flags, int mode, int succeed, 
  26.                       int error, char *string);
  27. #else
  28. static void openTest();
  29. static void checkOpen();
  30. #endif
  31.  
  32. void
  33. main(argc, argv)
  34.     int argc;
  35.     char **argv;
  36. {
  37.  
  38.     progname = argv[0];
  39.     openTest();
  40.     exit((errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
  41. }
  42.  
  43. static void
  44. checkOpen(pathname, flags, mode, succeed, err, string)
  45.     char *pathname;
  46.     int flags;
  47.     int mode;
  48.     int succeed;
  49.     int err;
  50.     char *string;
  51. {
  52.     int fd;
  53.  
  54.     if ((fd = open(pathname, flags, mode)) == -1) {
  55.     if (succeed == SHOULD_SUCCEED) {
  56.         (void) fprintf(stderr, "%s: %s\n", progname, string);
  57.         (void) fprintf(stderr, "Open failed: %s\n", strerror(errno));
  58.         ++errors;
  59.         return;
  60.     }
  61.     if (errno != err) {
  62.         (void) fprintf(stderr, "%s: %s\n", progname, string);
  63.         (void) fprintf(stderr, "Wrong errno (%d): %s\n",
  64.         errno, strerror(errno));
  65.         (void) fprintf(stderr, "Should be (%d): %s\n",
  66.         err, strerror(err));
  67.     }
  68.     return;
  69.     }
  70.     if (succeed == SHOULD_FAIL) {
  71.     (void) fprintf(stderr, "%s: %s\n", progname, string);
  72.     (void) fprintf(stderr, "Open succeeded, should have failed\n");
  73.     ++errors;
  74.     }
  75.     if (close(fd)) {
  76.     (void) fprintf(stderr, "%s: close failed: %s\n",
  77.         progname, strerror(errno));
  78.     ++errors;
  79.     }
  80.     return;
  81. }
  82.  
  83. static void
  84. openTest()
  85. {
  86.     char tempfile[MAXPATHLEN];
  87.     char longname[2 * MAXPATHLEN];
  88.     int fd, fd2;
  89.  
  90.     /*
  91.      *      Test bad pathname argument.
  92.      */
  93.     checkOpen((char *) -1, 0, 0, SHOULD_FAIL, EFAULT, "open(-1, 0, 0)");
  94.     checkOpen((char *) NULL, 0, 0, SHOULD_FAIL, EFAULT, "open(NULL, 0, 0)");
  95.  
  96.     /*
  97.      *      Check long filename
  98.      */
  99.     memset(longname, 'x', sizeof(longname));
  100.     checkOpen(longname, O_RDWR|O_CREAT, 0666, SHOULD_FAIL, ENAMETOOLONG,
  101.     "pathname too long");
  102.  
  103.     /*
  104.      *      Create and open a temporary file.
  105.      */
  106.     if (tmpnam(tempfile) != tempfile) {
  107.     (void) fprintf(stderr, "tmpnam() returned an incorrect value.\n");
  108.     ++errors;
  109.     return;
  110.     }
  111.     checkOpen(tempfile, O_CREAT, 0, SHOULD_SUCCEED, 0,
  112.     "Create and open a temporary file");
  113.  
  114.     /*
  115.      *      Try to do an exclusive open of an existing file.
  116.      */
  117.     checkOpen(tempfile, O_CREAT|O_EXCL, 0, SHOULD_FAIL, EEXIST,
  118.     "Exclusive open of existing file");
  119.  
  120.     /*
  121.      *      Open a file for reading, without read permission.
  122.      */
  123.     checkOpen(tempfile, O_RDONLY, 0, SHOULD_FAIL, EACCES,
  124.               "Open file for reading with no read permission");
  125.  
  126.     if (chmod(tempfile, 0444)) {
  127.     (void) fprintf(stderr, "chmod 0444 of %s failed: %s\n",
  128.         tempfile, strerror(errno));
  129.     ++errors;
  130.     }
  131.  
  132.     /*
  133.      *      Open a file for reading.
  134.      */
  135.     checkOpen(tempfile, O_RDONLY, 0, SHOULD_SUCCEED, 0,
  136.     "Open file for reading");
  137.  
  138.     /*
  139.      *      Open a file for writing without write permission.
  140.      */
  141.     checkOpen(tempfile, O_WRONLY, 0, SHOULD_FAIL, EACCES,
  142.               "Open file for writing");
  143.  
  144.     if (chmod(tempfile, 0666)) {
  145.     (void) fprintf(stderr, "chmod 0666 of %s failed: %s\n",
  146.         tempfile, strerror(errno));
  147.     ++errors;
  148.     }
  149.  
  150.     /*
  151.      *      Open a file for writing with write permission.
  152.      */
  153.     checkOpen(tempfile, O_WRONLY, 0, SHOULD_SUCCEED, 0,
  154.               "Open file for writing");
  155.  
  156.  
  157.     /*
  158.      *      Open a busy executable for writing
  159.      */
  160.     checkOpen("/bin/csh", O_WRONLY, 0, SHOULD_FAIL, ETXTBSY,
  161.           "Open of /bin/csh for writing");
  162.  
  163.     /*
  164.      *      open a  directory for writing
  165.      */
  166.     checkOpen(".", O_WRONLY, 0, SHOULD_FAIL, EISDIR,
  167.     "open a  directory for writing!\n");
  168.  
  169.     if (unlink(tempfile)) {
  170.     (void) fprintf(stderr, "Can't unlink %s: %s\n",
  171.         tempfile, strerror(errno));
  172.     ++errors;
  173.     }
  174.     return;
  175. }
  176.  
  177.